<?php
class ScglAction extends BaseAction {
 

	function index() {
	
	 
	   
	  $this->assign(mall,  isset($_REQUEST['malltype'])?json_encode($this->getdbleveldata($_REQUEST['malltype'])):0);
	  $this->assign(malltype,isset($_REQUEST['malltype'])?$_REQUEST['malltype']:0);
	  
		$this->display();
		}
   function qbsc(){
   
   $this->display();
   
   }
   function scfl(){
   
   $this->display();
   
   }
   // linkageSelfanlimall 异步获取商城数据
   function register($username,$webid,$web){
      $user=D('User');
	  //INSERT INTO `duoduo_user` (`id`, `ddusername`, `ddpassword`, `realname`, `regtime`, `regip`, `loginnum`, `lastlogintime`, `lasttixian`, `alipay`, `email`, `money`, `jifenbao`, `jifen`, `txstatus`, `dhstate`, `level`, `tjr`, `tjr_over`, `mobile`, `mobile_test`, `qq`, `pass_question`, `pass_answer`, `yitixian`, `fxb`, `jihuo`, `hart`, `tbyitixian`, `ucid`, `hytx`, `signtime`, `tenpay`, `bank_name`, `tbtxstatus`, `txtool`, `bank_code`) VALUES
//(1, 'ebuy361', 'f4b477237d342b0d6f72b8c984a33722', NULL, '2013-08-26 15:29:35', '127.0.0.1', 1, '2013-08-26 15:29:35', 0, NULL, '326815462@qq.com', 0.00, 0.00, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0.00, 0, 1, 0, 0.00, 0, 0, 0, NULL, NULL, 0, 1, '0');
     $data['ddusername']=$username;
	 $data['ddpassword']=md5('13631520471');
	 $data['regtime']=date('Y-m-d H:i:s');
	 $data['regip']=get_client_ip();
	 $data['loginnum']=1;
	 $data['lastlogintime']=date('Y-m-d H:i:s');
	 $data['jihuo']=1;
	 $data1['webid']=$webid;
	 $data1['web']=$web;
	 $data1['webname']=$username;
	if( $user->add($data)){
	 $data2=$user->field('id')->where("ddusername='".$data['ddusername']."'")->select();
	 $_SESSION['id']=$data2[0]['id'];
	 $data1['uid']=$data2[0]['id'];
	 $this->apilogin->add($data1);
	// dump($this->apilogin->select());
   redirect('/');
	 }
 
   }
   function login(){
   $user=D('User')->field('id')->where("ddusername='".$_POST['username']."'AND email='".$_POST['useremail']."'AND ddpassword='".md5($_POST['userpassword'])."'")->select();  
     if(count($user)>0){
	  $_SESSION['id']=$user[0]['id'];
	   $this->ajaxReturn($_SESSION['id'],"","");}
	   else{
	    $this->ajaxReturn("您的账号密码错误","","");
	   }
 
   }
   function getmall() {
       // $this->malldata=$this->getdbleveldata($_GET['id']);
	    $this->ajaxReturn($this->getdbleveldata($_GET['id']),"","");
	
 
   }
 
    function getmall_order()
	{  
	      /*'商城名称', name: 'mallname', 
		?'收益比例', name: 'sybl', 
        '购物金额', name: 'sales'
	    '总收益', name: 'commission', 
        '购物收益', name: 'gusy', 
		'业务收益', name: 'ywsy', 
		'分红收益', name: 'fhsy', 
		'商城人气', name: 'scrq', 
	    '商城标志', name: 'mallimg',
		'去购物赚收益', name: 'mallurl',
	    '业务链接', name: 'ywurl',
	  */
	 
	  $table1=array();
	  $j=0;
	  foreach($this->getdbleveldata($_POST['id']) as $ke => $val){
	    $val1=$this->object2array($val);
	      $table1[$j]['mallname']=$val1['name'];
		  $table1[$j]['sybl']=0.7;
		  $table1[$j]['sales']=round($this->mall_order->field('sales')->where("adid='".$ke."'")->sum('sales')+300000+rand(1,10000)+$this->randomFloat(0,1),2);
		  $table1[$j]['commission']=round($this->mall_order->field('commission')->where("adid='".$ke."'")->sum('commission')+300000*0.1+$this->randomFloat(0,1),2);
	      $table1[$j]['gusy']=round($table1[$j]['commission']*0.2,2);
		  $table1[$j]['ywsy']=round($table1[$j]['commission']*0.3,2);
		  $table1[$j]['fhsy']=round($table1[$j]['commission']*0.1,2);
		  $table1[$j]['scrq']=$this->mall_order->field('id')->where("adid='".$ke."'")->Count('id')+50000+rand(1,10000);
		   $table1[$j]['mallimg']="<img src='".$val1['img']."'/>";
	      $table1[$j]['mallurl']="<a href='".$val1['url'].(isset($_SESSION['id'])?$_SESSION['id']:0)."'>去购物</a>";
		  $table1[$j]['ywurl']=$val1['url'].'y'.(isset($_SESSION['id'])?$_SESSION['id']:0);
		  $j++;
		 } 
		
		 $data="{Rows:".json_encode($table1).",Total:".count($table1)."}";
		
	     $this->ajaxReturn($data,"","");
	  /*
	  $tj= array();
	  $tj['sales']=$this->mall_order->field('sales')->where("mall_name='".$_POST['name']."'")->sum('sales');
	  $tj['commission']=$this->mall_order->field('commission')->where("mall_name='".$_POST['name']."'")->sum('commission');
	  $tj['fxje']=$this->mall_order->field('fxje')->where("mall_name='".$_POST['name']."'")->sum('fxje');
      $table1=$this->mall_order->query("SELECT order_time,sales , commission, fxje,(commission-fxje) as fuwufei FROM  __TABLE__ WHERE mall_name='".$_POST['name']."'");
		foreach($table1 as $k => $v)
		{
		  $table1[$k]['order_time']=date("Y-m-d",$v['order_time']);
		}
		 $data="{Rows:".json_encode($table1).",Total:".count($table1)."}";
	     $this->ajaxReturn($data,$tj,"");
	 */
	 
	}
	function getusermall_order()
	{ 
	     /*'商城名称', name: 'mallname', 
		?'收益比例', name: 'sybl', 
        '购物金额', name: 'sales'
	    '总收益', name: 'commission', 
        '购物收益', name: 'gusy', 
		'业务收益', name: 'ywsy', 
		'分红收益', name: 'fhsy', 
		'商城人气', name: 'scrq', 
	    '商城标志', name: 'mallimg',
		'去购物赚收益', name: 'mallurl',
	    '业务链接', name: 'ywurl',
	  */
	 
	  $table1=array();
	  $j=0;
	  foreach($this->getdbleveldata($_POST['id']) as $ke => $val){
	    $val1=$this->object2array($val);
	      $table1[$j]['mallname']=$val1['name'];
		  $table1[$j]['sybl']=0.7;
		  $table1[$j]['sales']=round($this->mall_order->field('sales')->where("adid='".$ke."'AND uid='".$_SESSION['id']."'")->sum('sales'),2);
		  $table1[$j]['commission']=round($this->mall_order->field('commission')->where("adid='".$ke."'AND uid='".$_SESSION['id']."'")->sum('commission'),2);
	      $table1[$j]['gusy']=round($table1[$j]['commission']*0.2,2);
		  $table1[$j]['ywsy']=round($table1[$j]['commission']*0.3,2);
		  $table1[$j]['fhsy']=round($table1[$j]['commission']*0.1,2);
		   $table1[$j]['mallimg']="<img src='".$val1['img']."'/>";
	      $table1[$j]['mallurl']="<a href='".$val1['url'].(isset($_SESSION['id'])?$_SESSION['id']:0)."'>去购物</a>";
		  $table1[$j]['ywurl']=$val1['url'].'y'.$this->user_id;
		  $j++;
		 } 
		  
		//  print_r($this->malldata);
		 $data="{Rows:".json_encode($table1).",Total:".count($table1)."}";
		
	     $this->ajaxReturn($data,"","");
	 
	 
	
	}
	public function slogin($type = null){
		empty($type) && $this->error('参数错误');

		//加载ThinkOauth类并实例化一个对象
		import("ORG.ThinkSDK.ThinkOauth");
		$sns  = ThinkOauth::getInstance($type);

		//跳转到授权页面
		redirect($sns->getRequestCodeURL());
	}

	//授权回调地址
	public function callback($type = null, $code = null){
		(empty($type) || empty($code)) && $this->error('参数错误');
		
		//加载ThinkOauth类并实例化一个对象
		import("ORG.ThinkSDK.ThinkOauth");
		$sns  = ThinkOauth::getInstance($type);

		//腾讯微博需传递的额外参数
		$extend = null;
		if($type == 'tencent'){
			$extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
		}

		//请妥善保管这里获取到的Token信息，方便以后API调用
		//调用方法，实例化SDK对象的时候直接作为构造函数的第二个参数传入
		//如： $qq = ThinkOauth::getInstance('qq', $token);
		$token = $sns->getAccessToken($code , $extend);
          
		//获取当前登录用户信息
		if(is_array($token)){
		   
		  $user1=$this->apilogin->where("webid='".$token['openid']."' AND web='".$type."'")->select();
		  if(count($user1)>0){
		   $_SESSION['id']=$user1[0]['uid'];
			redirect('/');
		  }
		  else{
		  $user_info = A('Type', 'Event')->$type($token);
		  //dump($user_info);
		 // echo $user_info['nick'];
		   $this->register($user_info['nick'],$token['openid'],$type);
            }
		}
	}
}